package com.yang.seckillsys.controller;


import com.yang.seckillsys.pojo.Goods;
import com.yang.seckillsys.pojo.Order;
import com.yang.seckillsys.pojo.User;
import com.yang.seckillsys.service.IGoodsService;
import com.yang.seckillsys.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YANGLIYUAN
 * @since 2021-04-17
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    @Resource
    private IOrderService orderService;
    @Resource
    private IGoodsService goodsService;

    @RequestMapping(value = "/toList/{userId}",produces = "text/html;charset=utf-8")
    public String toList(Model model, User user, @PathVariable Long userId, HttpServletRequest request, HttpServletResponse response) {
        //若 user 对象为空，则返回登录页
/*        if (user == null) {
            WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
            String login = thymeleafViewResolver.getTemplateEngine().process("login", webContext);
            return login;
        }*/

        //查询用户对应的订单列表
        List<Order> orderList = orderService.findOrder(userId);
        model.addAttribute("oderList",orderList);
        return "orderList";
    }

    @RequestMapping(value = "/toDetail/{id}",produces = "text/html;charset=utf-8")
    public String toDetail(Model model,@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {
        Order order = orderService.findOrderById(id);
        long goodsId = order.getGoodsId();
        Goods goods = goodsService.findGoodsByGoodsId(goodsId);
        model.addAttribute("order", order);
        model.addAttribute("goods", goods);
        return "orderDetail";
    }

}
